#include <StdAfx.h>

#include "stdio.h"
#include "rs232_util.h"

void putData(BYTE* buffer, BYTE* tempData, int tempDataLen) {
     int i=0;
     for(i=0; i<tempDataLen; i++) {
         buffer[i] = tempData[i];         
     }     
}

void printError(char* errMsg){
	printf("%s", errMsg);
	system("pause");
}

int matchByte(BYTE input, int* num, int arrayLen, BYTE* pattern) {
    int flag=0;
    if(input == pattern[*num]){
        (*num)++;
    }else {
        (*num) = 0;      
    }
    if(*num == arrayLen){
        flag = 1;   
        (*num) = 0;    
    }
    return flag;
}

void putSingleByte(BYTE *buf, BYTE *input, int* CurrentLocation) {
    *buf = *input;
    (*CurrentLocation)++;     
}

void printLine(BYTE forPrint[], int *CurrentLocation) {
    int i = 0;
    for(i=0; i<*CurrentLocation; i++) {
          //printf("%02x-", forPrint[i]); 
          printf("%c", forPrint[i]);
    }    
    //printf("\n"); 
    *CurrentLocation = 0;
}

void parseData(BYTE forPrint[], int CurrentLocation) {
	
	int match_GPRMC_num=0;
    BYTE match_GPRMC[] = {0x24, 0x47, 0x50, 0x52, 0x4D, 0x43};
	int match_GPGGA_num=0;
    BYTE match_GPGGA[] = {0x24, 0x47, 0x50, 0x47, 0x47, 0x41};
	int HeadLength = 6;//ex. the match_GPRMC[] array size

	int i_input = 0;
	for(i_input = 0; i_input<HeadLength; i_input++){   
		if(matchByte(forPrint[i_input], &match_GPRMC_num, HeadLength, match_GPRMC)){
			//printf("[Match GPRMC]");
		}
		if(matchByte(forPrint[i_input], &match_GPGGA_num, HeadLength, match_GPGGA)){
			//printf("[Match GPGGA]");
			analyzeGPGGA(forPrint, CurrentLocation);
		}
	}
}

void analyzeGPGGA(BYTE forPrint[], int CurrentLocation) {
	//ayalyze type: char
	char tmpSection[15][30];
	SeperateByComma(forPrint, CurrentLocation, tmpSection);
	//printf("%d", tmpSection[0][6]);
	//printf("[%d]", tmpSection[1][9]);

}

void SeperateByComma(BYTE forPrint[], int CurrentLocation, char tmpSection[][30]) {
	int section_num = SectionNumber(forPrint, CurrentLocation);
	BYTE buffer[512];
	CommaReplace(buffer, forPrint,CurrentLocation);
	//printf("[%d]", buffer[6]);
	int i=0;
	for(i=0; i< section_num; i++) {
		if(i==0){//section 0
			strncpy(tmpSection[i], (char*)buffer, SectionLength(buffer, CurrentLocation, -1, 0));
			//printf("[#%d]", buffer[6]);
			tmpSection[i][SectionLength(buffer, CurrentLocation, -1, 0)] = NULL;
			//printf("[#%d]", tmpSection[0][6]);
		}else{
			strncpy(tmpSection[i], (char*)buffer + NCommaPosition(buffer, CurrentLocation,i-1)+1,
				    SectionLength(buffer, CurrentLocation, i-1, i));
			if(i < section_num -1) {
				tmpSection[i][SectionLength(buffer, CurrentLocation, i-1, i)] = NULL;//#############################
			}
		}
	}
}

int NCommaPosition(BYTE buffer[], int CurrentLocation, int n) {
	//n start from 0
	if(n == -1) {
		return -1;
	}
	int check_n = 0;
	int i=0;
	for(i=0; i<CurrentLocation; i++) {
		if(buffer[i] == NULL) {
			if(check_n == n) {
				return i;//find N-comma position
			}
			check_n++;
		}
	}
	return CurrentLocation;
}

int SectionLength(BYTE buffer[], int CurrentLocation, int StartN, int EndN) {
	if(StartN == -1 || EndN == 0) {//handle the section before 0-comma
		return NCommaPosition(buffer, CurrentLocation, EndN) - NCommaPosition(buffer, CurrentLocation, StartN) -1;
	}else{
		return NCommaPosition(buffer, CurrentLocation, EndN) - NCommaPosition(buffer, CurrentLocation, StartN) -1;
	}
}

int SectionNumber(BYTE buffer[], int CurrentLocation) {
	int comma_num = 0;
	
    int i = 0;
	for(i=0; i<CurrentLocation; i++) {
		if(buffer[i] == COMMA_HEX) {//find comma
			comma_num++;
		}
	}
	return comma_num + 1;//section_num = comma_num + 1
}

void CommaReplace(BYTE *buffer, BYTE forPrint[], int CurrentLocation) {
    int i = 0;
	for(i=0; i<CurrentLocation; i++) {
		if(forPrint[i] == COMMA_HEX) {//find comma
			buffer[i] = 0x0;//'\0'
		}else {
			buffer[i] = forPrint[i];
		}
	}
}